# defines for build #
CXX_DEFINES:=
# compiler #
CXX:=gcc
CXXFLAGS:=-I ./include -nostdlib -Wall -ffreestanding -DDEBUG_INFO -DDEBUG_WARN -DDEBUG_ERROR
# linker #
LD:=ld
LDFLAGS:=-T linker.ld -Map link.map
# sources #
AS:=as
NASM16:=nasm
NASM_16SRCS=$(shell find -mindepth 1 -maxdepth 3 -name "*.nasm16")
AS_32SRCS:=$(shell find -mindepth 1 -maxdepth 3 -name "*.s")
C_SRCS:=$(shell find -mindepth 1 -maxdepth 3 -name "*.c")
# object files #
OBJS:=$(patsubst %.s,%.o,$(AS_32SRCS)) $(patsubst %.nasm16,%.o,$(NASM_16SRCS)) $(patsubst %.c,%.o,$(C_SRCS))
# final output #
FINALBIN:=kernel

# build the full kernel
default: $(FINALBIN)

# clean up
clean:
	rm $(OBJS) *.bin

# link command
$(FINALBIN) : $(OBJS)
	@echo Linking
	@echo $(shell find -mindepth 1 -maxdepth 3 -name "*.o")
	@$(LD) $(LDFLAGS) -o $(FINALBIN) $(shell find -mindepth 1 -maxdepth 3 -name "*.o")
	#@strip kernel

# C Source
# -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wredundant-decls -Winline -Wno-long-long -Wconversion
# the above = nightmare!
%.o : %.c
	@echo Compiling $<
	@$(CXX) $(CXX_DEFINES) $(CXXFLAGS) -c $<

# assembly
%.o : %.s
	@echo Assembling $<
	$(AS) $< -o $@

# assembly for 16BIT code
%.o : %.nasm16
	@echo [VM86]Assembling $<
	@$(NASM16) -f elf $<

install:
	@echo --Installing Kernel Image--
	cp kernel $(TARGETDIR)/kernel